diff options
| author | Factiven <[email protected]> | 2023-09-13 00:45:53 +0700 |
|---|---|---|
| committer | GitHub <[email protected]> | 2023-09-13 00:45:53 +0700 |
| commit | 7327a69b55a20b99b14ee0803d6cf5f8b88c45ef (patch) | |
| tree | cbcca777593a8cc4b0282e7d85a6fc51ba517e25 /pages/api/auth/[...nextauth].js | |
| parent | Update issue templates (diff) | |
| download | moopa-7327a69b55a20b99b14ee0803d6cf5f8b88c45ef.tar.xz moopa-7327a69b55a20b99b14ee0803d6cf5f8b88c45ef.zip | |
Update v4 - Merge pre-push to main (#71)
* Create build-test.yml
* initial v4 commit
* update: github workflow
* update: push on branch
* Update .github/ISSUE_TEMPLATE/bug_report.md
* configuring next.config.js file
Diffstat (limited to 'pages/api/auth/[...nextauth].js')
| -rw-r--r-- | pages/api/auth/[...nextauth].js | 60 |
1 files changed, 57 insertions, 3 deletions
diff --git a/pages/api/auth/[...nextauth].js b/pages/api/auth/[...nextauth].js index f270e7a..da78d07 100644 --- a/pages/api/auth/[...nextauth].js +++ b/pages/api/auth/[...nextauth].js @@ -1,6 +1,5 @@ import NextAuth from "next-auth"; -import { GET_CURRENT_USER } from "../../../queries"; -import { ApolloClient, InMemoryCache } from "@apollo/client"; +import { ApolloClient, InMemoryCache, gql } from "@apollo/client"; const defaultOptions = { watchQuery: { @@ -40,7 +39,24 @@ export const authOptions = { url: process.env.GRAPHQL_ENDPOINT, async request(context) { const { data } = await client.query({ - query: GET_CURRENT_USER, + query: gql` + query { + Viewer { + id + name + avatar { + large + medium + } + bannerImage + mediaListOptions { + animeList { + customLists + } + } + } + } + `, context: { headers: { Authorization: "Bearer " + context.tokens.access_token, @@ -48,11 +64,47 @@ export const authOptions = { }, }); + const userLists = data.Viewer.mediaListOptions.animeList.customLists; + + let custLists = userLists || []; + + if (!userLists?.includes("Watched using Moopa")) { + custLists.push("Watched using Moopa"); + const fetchGraphQL = async (query, variables) => { + const response = await fetch("https://graphql.anilist.co/", { + method: "POST", + headers: { + "Content-Type": "application/json", + Authorization: context.tokens.access_token + ? `Bearer ${context.tokens.access_token}` + : undefined, + }, + body: JSON.stringify({ query, variables }), + }); + return response.json(); + }; + + const customLists = async (lists) => { + const setList = ` + mutation($lists: [String]){ + UpdateUser(animeListOptions: { customLists: $lists }){ + id + } + } + `; + const data = await fetchGraphQL(setList, { lists }); + return data; + }; + + await customLists(custLists); + } + return { token: context.tokens.access_token, name: data.Viewer.name, sub: data.Viewer.id, image: data.Viewer.avatar, + list: data.Viewer.mediaListOptions.animeList.customLists, }; }, }, @@ -64,6 +116,8 @@ export const authOptions = { id: profile.sub, name: profile?.name, image: profile.image, + list: profile?.list, + version: "1.0.1", }; }, }, |